unit RenameProfile;

interface

uses
  Database,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons;

type
  TFormRenameProfile = class(TForm)
    Label1: TLabel;
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    EditPlayer: TEdit;
    procedure EditPlayerChange(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EditPlayerKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
    player, mud, replacement: string;
  end;

var
  FormRenameProfile: TFormRenameProfile;

implementation

{$R *.DFM}

procedure TFormRenameProfile.FormCreate(Sender: TObject);
begin
  replacement:='';
end;

procedure TFormRenameProfile.EditPlayerChange(Sender: TObject);
begin
  if length(EditPlayer.text) > 0 then BtnOK.enabled:=true
  else BtnOK.enabled:=false;
end;

procedure TFormRenameProfile.BtnCancelClick(Sender: TObject);
begin
  replacement:='';
  close;
end;

procedure TFormRenameProfile.BtnOKClick(Sender: TObject);
var
  validname, didfind: boolean;
  work: string;
  i: integer;
begin
  if EditPlayer.text = '' then begin
    messagedlg('You must enter a player name',mtinformation,[mbok],0);
    EditPlayer.setfocus;
    exit;
  end;
  if EditPlayer.text = lowercase(player) then begin
    messagedlg('That''s the same player name',mtinformation,[mbok],0);
    EditPlayer.selectall;
    EditPlayer.setfocus;
    exit;
  end;
  validname:=true;
  work:=EditPlayer.text;
  for i:=1 to length(work) do begin
    if (ord(work[i]) < 58) or (ord(work[i]) > 122) then begin
      validname:=false;
      break;
    end;
  end;
  if not validname then begin
    messagedlg('Player names must contain letters only',
      mtinformation,[mbok],0);
    EditPlayer.setfocus;
    exit;
  end;
  work:=Datawell.cap(work);
  didfind:=false;
  with Datawell.TablePlayers do begin
    refresh;
    wwfilter.clear;
    filterActivate;
    active:=true;
    first;
    while (not EOF) and (not didfind) do begin
      if (Datawell.TablePlayers.fieldbyname('MUD').asstring = mud) and
        (Datawell.TablePlayers.fieldbyname('Player').asstring = work) then didfind:=true
      else next;
    end;
    if didfind then begin
      if messagedlg(Datawell.cap(work)+' already exists in '+mud+'.  Replace it?',
        mtconfirmation,[mbyes,mbno],0) = mrno then begin
        EditPlayer.selectall;
        EditPlayer.setfocus;
        exit;
      end;
//      Datawell.DeleteProfile('xxx');
    end;
  end;
  replacement:=EditPlayer.text;
  close;
end;

procedure TFormRenameProfile.EditPlayerKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    BtnOK.click;
    key:=#0;
  end;
end;

end.
